package cn.dnf.pet.controller;

import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.LoginContext;
import cn.dnf.basic.util.PageList;
import cn.dnf.pet.domain.Pet;
import cn.dnf.pet.query.PetQuery;
import cn.dnf.pet.service.IPetService;
import cn.dnf.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/pet")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class PetController {

    @Autowired
    private IPetService PetService;

    /**
     * http://localhost:8080/Pet  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有宠物信息" )
    public List<Pet> findAll(HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return PetService.loadAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/Pet/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public Pet findOne(@PathVariable("id") Long id){
        return PetService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            PetService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/Pet
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updateOrAdd(@RequestBody Pet pet){

        try {
            if(pet.getId()==null){
                // 添加操作
                PetService.save(pet);
            }else{
                // 修改操作
                PetService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        try {
            // 哈哈哈
            return PetService.pageQuery(query);
            // ....
            // .....
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Pet>();
        }
    }


    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            PetService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public JsonResult onSale(@RequestBody List<Long> ids){

        try {
            PetService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offSale(@RequestBody List<Long> ids){

        try {
            PetService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

}
